package my.test.hencodertest.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import my.test.hencodertest.R;

/**
 * Created by 丁旭 on 2017/12/15.
 */

public class PaperFolding extends View {
    private static final String TAG = "PaperFolding";
    RectF rect;
    Bitmap bitmap;
    int degree = 0;
    float foldingSize = 0;

    public int getDegree() {
        return degree;
    }

    public void setDegree(int degree) {
        this.degree = degree;
        invalidate();
    }

    public float getFoldingSize() {
        return foldingSize;
    }

    public void setFoldingSize(float foldingSize) {
        this.foldingSize = foldingSize;
        invalidate();
    }

    public PaperFolding(Context context) {
        super(context);
        init();
    }

    public PaperFolding(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PaperFolding(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.email);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d(TAG, "onLayout");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "onDraw: foldingsize=" + foldingSize + ",degree=" + degree);

        Matrix matrix = new Matrix();

        float diameter = (float) Math.sqrt(getWidth() * getWidth() + getHeight() * getHeight());

        RectF normalSide = new RectF();
        normalSide.left = (getWidth() - diameter) / 2;
        normalSide.top = (getHeight() - diameter) / 2;
        normalSide.right = normalSide.left + diameter;
        normalSide.bottom = getHeight() / 2;

        RectF stretchSide = new RectF();
        stretchSide.left = (getWidth() - diameter) / 2;
        stretchSide.top = getHeight() / 2;
        stretchSide.right = stretchSide.left + diameter;
        stretchSide.bottom = stretchSide.top + diameter / 2;

        float[] pointSrc = {stretchSide.left, stretchSide.top, stretchSide.right, stretchSide.top, stretchSide.left, stretchSide.bottom, stretchSide.right, stretchSide.bottom};
        float[] pointDest = {stretchSide.left, stretchSide.top, stretchSide.right, stretchSide.top, stretchSide.left - foldingSize, stretchSide.bottom - foldingSize, stretchSide.right + foldingSize, stretchSide.bottom - foldingSize};


        canvas.save();
        canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
        canvas.clipRect(normalSide);
        canvas.rotate(-degree, getWidth() / 2, getHeight() / 2);
        canvas.drawBitmap(bitmap, (getWidth() - bitmap.getWidth()) / 2, (getHeight() - bitmap.getHeight()) / 2, null);
        canvas.restore();

        matrix.reset();
        matrix.setPolyToPoly(pointSrc, 0, pointDest, 0, 4);

        canvas.save();
        canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
        canvas.clipRect(stretchSide);
        canvas.concat(matrix);
        canvas.rotate(-degree, getWidth() / 2, getHeight() / 2);
        canvas.drawBitmap(bitmap, (getWidth() - bitmap.getWidth()) / 2, (getHeight() - bitmap.getHeight()) / 2, null);
        canvas.restore();
    }

//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//
//        Matrix matrix = new Matrix();
////        float[] pointSrc = {rect.left, rect.top, rect.right, rect.top, rect.left, rect.bottom, rect.right, rect.bottom};
////        float[] pointDest = {rect.left + 10, rect.top + 10, rect.right - 10, rect.top - 10, rect.left + 10, rect.bottom - 10, rect.right - 10, rect.bottom + 10};
//
//        int left = bitmap.getWidth() / 2;
//        int top = 0;
//        int right = bitmap.getWidth();
//        int bottom = bitmap.getHeight();
//
//        canvas.save();
//        //canvas.rotate(degree);
//
//        canvas.clipRect(0, 0, bitmap.getWidth() / 2, bitmap.getHeight());
//        canvas.drawBitmap(bitmap, 0, 0, null);
//        canvas.restore();
//
//        float[] pointSrc = {left, top, right, top, left, bottom, right, bottom};
//        float[] pointDest = {left, top, right - 50, top - 50, left, bottom, right - 50, bottom + 50};
//
//        matrix.reset();
//        matrix.setPolyToPoly(pointSrc, 0, pointDest, 0, 4);
//
//        canvas.save();
//        canvas.concat(matrix);
//        canvas.clipRect(left, top, right, bottom);
//        canvas.drawBitmap(bitmap, 0, 0, null);
//        canvas.restore();
//    }
}
